package gonz.data.swim;

import gonz.common.Finder;
import gonz.common.Waiter;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

public class Swim {
    public static final Logger log = Logger.getLogger(Swim.class);
    public static final String BRAND_NAME_XPATH = "//div[@id='brand-name']/h3/cufon/cufontext";
    public static final String SWIM_NAME_FEATURE_GRP_XPATH = "//div[@id='content']/div/div[2]/div[2]/div[2]/div[2]/div/h1/cufon/cufontext";
    public static final String SWIM_NAME_XPATH = "//div[@id='content']/div/div[2]/div[2]/div/div[2]/div/h1/cufon/cufontext";
    public static final String CURRENT_SWIM_NUMBER_ID = "the_prev_link";
    public static final String NEXT_SWIM_NUMBER_ID = "the_next_link";
    private WebDriver webDriver;
    private Top top;
    private String brandName;
    private String name;
    private String link;
    private String currentSwimNumber;
    private String nextSwimNumber;

    public Swim(WebDriver webDriver, String imgFolder) {
        this.webDriver = webDriver;
        this.link = webDriver.getCurrentUrl();
        this.currentSwimNumber = grabCurrentSwimNumber();
        this.nextSwimNumber = grabNextSwimNumber();
        this.brandName = grabBrandName();
        this.name = grabSwimName();
        this.top = new Top(webDriver, this, imgFolder);
    }

    public String getLink() {
        return link;
    }

    public String getBrandName() {
        return brandName;
    }

    public String getName() {
        return name;
    }

    public Top getTop() {
        return top;
    }

    public String getCurrentSwimNumber() {
        return currentSwimNumber;
    }

    public String getNextSwimNumber() {
        return nextSwimNumber;
    }

    private String grabBrandName() {
        List<WebElement> brandElements = webDriver.findElements(By.xpath(BRAND_NAME_XPATH));
        if (brandElements == null || brandElements.size() == 0) {
            log.warn("Can't find brand name, swim link: " + getLink());
            return "unknown";
        }
        StringBuilder brandName = new StringBuilder();
        for (WebElement brandElement : brandElements) {
            brandName.append(brandElement.getText())
                    .append(" ");
        }
        log.debug("Brand name has been grabbed: " + brandName.toString());
        return brandName.toString();
    }

    /**
     * Grab swim name from page.
     *
     * @return
     */
    private String grabSwimName() {
        List<WebElement> swimNameElements = webDriver.findElements(By.xpath(SWIM_NAME_FEATURE_GRP_XPATH));
        if (swimNameElements == null || swimNameElements.size() == 0) {
            log.debug("Can't find swim name with feature group, trying find it with out feature group.");
            swimNameElements = Finder.waitAndFindElements(webDriver, By.xpath(SWIM_NAME_XPATH));
            if (swimNameElements == null || swimNameElements.size() == 0) {
                log.error("Can't find swim name, swim link: " + getLink());
                return "unknown";
            }
        }
        StringBuilder swimName = new StringBuilder();
        for (WebElement swimNameElement : swimNameElements) {
            swimName.append(swimNameElement.getText())
                    .append(" ");
        }
        log.debug("TwoPiecesSwim name has been grabbed: " + swimName.toString());
        return swimName.toString();
    }

    private String grabCurrentSwimNumber() {
        //WebElement numberElement = webDriver.findElement(By.id(CURRENT_SWIM_NUMBER_ID));
        WebElement numberElement = Waiter.waitElement(webDriver, By.id(CURRENT_SWIM_NUMBER_ID));
        String number = numberElement.getText();
        log.debug("TwoPiecesSwim number has been grabbed: " + number);
        if (StringUtils.isBlank(number)) {
            log.error("Can't get current item number. number: " + number + ", item link: " + getLink());
        }
        return number;
    }

    private String grabNextSwimNumber() {
        WebElement numberElement = webDriver.findElement(By.id(NEXT_SWIM_NUMBER_ID));
        String number = numberElement.getText();
        log.debug("TwoPiecesSwim number has been grabbed: " + number);
        return number;
    }

    public TwoPiecesSwim openNextSwim(String imgFolder) {
        webDriver.findElement(By.id(NEXT_SWIM_NUMBER_ID)).click();
        return new TwoPiecesSwim(webDriver, imgFolder);
    }

}
